class Solution {
public:
  int lengthOfLongestSubstring(string str) {

    if(str.length() == 0){
      return 0;
    }

    int ans = 0;
    int i = 0, j = 0;
    set<char> * s = new set<char>();

    while(j < str.length()){
      if(s->count(str[j]) == 0){
        s->insert(str[j]);
      }else{
        ans = max(ans, j - i);
        s->erase(str[i]);
        i++;
        // 因为最后统一j++，所以此处j--
        j--;
      }
      j++;
    }

    ans = max(j - i, ans);

    return ans;

  }
};